package com.fc.advbox.shiro.common;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SessionContext;
import org.apache.shiro.session.mgt.SessionFactory;
import org.apache.shiro.web.session.mgt.WebSessionContext;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by futurecreative on 2017/10/20.
 */
public class AdvSessionFactory implements SessionFactory {

    @Override
    public Session createSession(SessionContext initData) {
        AdvSession session = new AdvSession();
        if (initData != null) {
            WebSessionContext sessionContext = (WebSessionContext) initData;
            HttpServletRequest request = WebUtils.toHttp(sessionContext.getServletRequest());

            if (request != null) {
                String platform = request.getHeader("platform");
                session.setPlatform(AdvSession.Platform.fromString(platform));
            }

            String host = initData.getHost();

            if (host != null) {
                session.setHost(host);
            }
        }
        return session;
    }
}
